<?php

/**
 * CoreSettings actions.
 *
 * @package    ProdAssistant
 * @subpackage CoreSettings
 * @author     Paweł Świtalla
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class CoreSettingsActions extends sfActions
{
  public function executeCreate(sfWebRequest $request)
  {
    $this->form = new SettingsForm();
    $core = PACore::getInstance();
    $url = $core->generateAppUrl('Core', 'coreSettings');
    if($request->isMethod('post'))
    {
      $params = $request->getParameter('settings');
      $this->form->bind($params);
      if($this->form->isValid())
      {
        $setting = $this->form->save();
        if($request->isXmlHttpRequest())
        {
          $this->getResponse()->setContent('location@'.$url."#");
          return sfView::NONE;
        }
        else $this->redirect($url);
      }
    }
    if($request->isXmlHttpRequest())
    {
      $errors = '';
      foreach($this->form->getFormFieldSchema() as $field){
        if($field->hasError()){
          $errors .= ucfirst($field->getName()).': '.$field->getError()."\n";
        }
      }
      $this->getResponse()->setContent('error@'.$errors."#");
      return sfView::NONE;
    }
    else $this->referer = $url;
  }
  
  public function executeDelete(sfWebRequest $request)
  {
    $core = PACore::getInstance();
    $url = $core->generateAppUrl('Core', 'coreSettings');
    $id = $request->getParameter('id');
    $setting = Doctrine_Core::getTable('Settings')->find($id);
    if($setting instanceof Settings)
    {
      $this->redirectIf($setting->delete(), $url);
    }
  }
  
  public function executeUpdate(sfWebRequest $request)
  {
    $setting = Doctrine_Core::getTable('Settings')->find($request->getParameter('id'));
    $this->form = new SettingsForm($setting);
    $core = PACore::getInstance();
    $url = $core->generateAppUrl('Core', 'coreSettings');
    if($request->isMethod('put'))
    {
      $params = $request->getParameter('settings');
      $this->form->bind($params);
      if($this->form->isValid())
      {
        $setting = $this->form->save();
        if($request->isXmlHttpRequest())
        {
          $this->getResponse()->setContent('location@'.$url."#");
          return sfView::NONE;
        }
        else $this->redirect($url);
      }
    }
    if($request->isXmlHttpRequest())
    {
      $errors = '';
      foreach($this->form->getFormFieldSchema() as $field){
        if($field->hasError()){
          $errors .= ucfirst($field->getName()).': '.$field->getError()."\n";
        }
      }
      $this->getResponse()->setContent('error@'.$errors."#");
      return sfView::NONE;
    }
    else $this->referer = $url;
  }
  
  public function executeIndex(sfWebRequest $request)
  {
  }
  
  public function executeCreateCategory(sfWebRequest $request)
  {
    $this->form = new SettingsCategoryForm();
    $core = PACore::getInstance();
    $url = $core->generateAppUrl('Core', 'coreSettings');
    if($request->isMethod('post'))
    {
      $params = $request->getParameter('settings_category');
      $this->form->bind($params);
      if($this->form->isValid())
      {
        $settingCategory = $this->form->save();
        if($request->isXmlHttpRequest())
        {
          $this->getResponse()->setContent('location@'.$url."#");
          return sfView::NONE;
        }
        else $this->redirect($url);
      }
    }
    if($request->isXmlHttpRequest())
    {
      $errors = '';
      foreach($this->form->getFormFieldSchema() as $field){
        if($field->hasError()){
          $errors .= ucfirst($field->getName()).': '.$field->getError()."\n";
        }
      }
      $this->getResponse()->setContent('error@'.$errors."#");
      return sfView::NONE;
    }
    else $this->referer = $url;
  }
  
  public function executeDeleteCategory(sfWebRequest $request)
  {
    $core = PACore::getInstance();
    $url = $core->generateAppUrl('Core', 'coreSettings');
    $id = $request->getParameter('id');
    $category = Doctrine_Core::getTable('SettingsCategory')->find($id);
    if($category instanceof SettingsCategory)
    {
      $this->redirectIf($category->delete(), $url);
    }
  }
  
  public function executeUpdateCategory(sfWebRequest $request)
  {
    $settingCategory = Doctrine_Core::getTable('SettingsCategory')->find($request->getParameter('id'));
    $this->form = new SettingsCategoryForm($settingCategory);
    $core = PACore::getInstance();
    $url = $core->generateAppUrl('Core', 'coreSettings');
    if($request->isMethod('put'))
    {
      $params = $request->getParameter('settings_category');
      $this->form->bind($params);
      if($this->form->isValid())
      {
        $settingCategory = $this->form->save();
        if($request->isXmlHttpRequest())
        {
          $this->getResponse()->setContent('location@'.$url."#");
          return sfView::NONE;
        }
        else $this->redirect($url);
      }
    }
    if($request->isXmlHttpRequest())
    {
      $errors = '';
      foreach($this->form->getFormFieldSchema() as $field){
        if($field->hasError()){
          $errors .= ucfirst($field->getName()).': '.$field->getError()."\n";
        }
      }
      $this->getResponse()->setContent('error@'.$errors."#");
      return sfView::NONE;
    }
    else $this->referer = $url;
  }
}
